import Vue from 'vue';
import VueI18n from 'vue-i18n';

// 注册VueI18n插件
Vue.use(VueI18n);

// 定义语言包
const messages = {
  // 英文语言包
  en: {
    // 通用词汇
    common: {
      search: 'Search',
      play: 'Play',
      pause: 'Pause',
      next: 'Next',
      previous: 'Previous',
      volume: 'Volume',
      add: 'Add',
      remove: 'Remove',
      save: 'Save',
      cancel: 'Cancel',
      confirm: 'Confirm',
      loading: 'Loading...',
      error: 'Error',
      success: 'Success',
      yes: 'Yes',
      no: 'No',
      ok: 'OK',
      welcome: 'Welcome',
      logout: 'Logout'
    },
    // 导航栏
    navbar: {
      logo: 'Music Player',
      home: 'Home',
      search: 'Search',
      playlist: 'My Playlist',
      settings: 'Settings',
      login: 'Login',
      register: 'Register'
    },
    // 登录页面
    login: {
      title: 'Login',
      username: 'Username',
      password: 'Password',
      rememberMe: 'Remember me',
      appTitle: 'Music Player',
      forgotPassword: 'Forgot password?',
      noAccount: 'Don\'t have an account?',
      registerHere: 'Register here',
      loginSuccess: 'Login successful!',
      loginFailed: 'Login failed, please check your username and password',
      placeholderUsername: 'Please enter your username',
      placeholderPassword: 'Please enter your password',
      button: 'Login',
      inputRequired: 'Username/Phone and password are required',
      registerSuccess: 'Registration successful! Please login.'
    },
    // 注册页面
    register: {
      title: 'Register',
      username: 'Phone Number',
      email: 'Email',
      password: 'Password',
      confirmPassword: 'Confirm Password',
      haveAccount: 'Already have an account?',
      loginHere: 'Login here',
      registerSuccess: 'Registration successful!',
      registerFailed: 'Registration failed',
      usernameRequired: 'Phone number is required',
      emailRequired: 'Email is required',
      emailInvalid: 'Invalid email format',
      passwordRequired: 'Password is required',
      passwordMinLength: 'Password must be at least 6 characters',
      passwordMatch: 'Passwords do not match',
      captcha: 'Verification Code',
      sendCaptcha: 'Send Code',
      captchaSent: 'Code sent!',
      captchaFailed: 'Failed to send code',
      captchaRequired: 'Verification code is required'
    },
    // 搜索页面
    search: {
      placeholder: 'Search songs, artists, albums...',
      searching: 'Searching...',
      search: 'Search',
      songs: 'Songs',
      artists: 'Artists',
      albums: 'Albums',
      noResults: 'No results found, please try other keywords',
      song: 'Song',
      artist: 'Artist',
      album: 'Album',
      duration: 'Duration',
      songsCount: ' songs',
      searchFailed: 'Search failed, please try again later',
      cannotPlay: 'This song cannot be played',
      playFailed: 'Failed to play song'
    },
    // 歌曲详情页面
    songDetail: {
      loading: 'Loading song details...',
      artist: 'Artist',
      album: 'Album',
      lyrics: 'Lyrics',
      recommendations: 'Recommendations',
      addToPlaylist: 'Add to playlist',
      download: 'Download',
      downloadNotImplemented: 'Download function not implemented yet',
      addedToPlaylist: 'Added to playlist',
      alreadyInPlaylist: 'This song is already in the playlist',
      play: 'Play',
      pause: 'Pause',
      favorite: 'Favorite',
      fetchFailed: 'Failed to fetch song details, please try again later',
      noLyrics: 'No lyrics available',
      addFailed: 'Add failed, please try again later',
      cannotPlay: 'Cannot play this song'
    },
    // 首页
    home: {
      popularPlaylists: 'Popular Playlists',
      recentSongs: 'Recent Songs',
      topCharts: 'Top Charts',
      recommendedForYou: 'Recommended For You',
      searchSongs: 'Search for songs, artists or albums',
      createPlaylist: 'Create Playlist'
    },
    // 播放器
    player: {
      playlists: 'Playlists',
      repeat: 'Repeat',
      shuffle: 'Shuffle',
      clearQueue: 'Clear Queue',
      autoplay: 'Autoplay',
      loop: 'Loop'
    }
  },
  // 中文语言包
  zh: {
    // 通用词汇
    common: {
      search: '搜索',
      play: '播放',
      pause: '暂停',
      next: '下一首',
      previous: '上一首',
      volume: '音量',
      add: '添加',
      remove: '移除',
      save: '保存',
      cancel: '取消',
      confirm: '确认',
      loading: '加载中...',
      error: '错误',
      success: '成功',
      yes: '是',
      no: '否',
      ok: '确定',
      welcome: '欢迎',
      logout: '退出登录'
    },
    // 导航栏
    navbar: {
      logo: '音乐播放器',
      home: '首页',
      search: '搜索',
      playlist: '我的播放列表',
      settings: '设置',
      login: '登录',
      register: '注册'
    },
    // 登录页面
    login: {
      title: '登录',
      username: '用户名',
      password: '密码',
      rememberMe: '记住我',
      appTitle: '音乐播放器',
      forgotPassword: '忘记密码？',
      noAccount: '还没有账号？',
      registerHere: '立即注册',
      loginSuccess: '登录成功！',
      loginFailed: '登录失败，请检查您的用户名和密码',
      placeholderUsername: '请输入用户名',
      placeholderPassword: '请输入密码',
      button: '登录',
      inputRequired: '用户名/手机号和密码不能为空',
      registerSuccess: '注册成功！请登录。'
    },
    // 注册页面
    register: {
      title: '注册',
      username: '手机号',
      email: '邮箱',
      password: '密码',
      confirmPassword: '确认密码',
      haveAccount: '已有账号？',
      loginHere: '立即登录',
      registerSuccess: '注册成功！',
      registerFailed: '注册失败',
      usernameRequired: '手机号不能为空',
      emailRequired: '邮箱不能为空',
      emailInvalid: '邮箱格式不正确',
      passwordRequired: '密码不能为空',
      passwordMinLength: '密码至少6个字符',
      passwordMatch: '两次输入的密码不一致',
      captcha: '验证码',
      sendCaptcha: '发送验证码',
      captchaSent: '验证码已发送！',
      captchaFailed: '验证码发送失败',
      captchaRequired: '验证码不能为空'
    },
    // 搜索页面
    search: {
      placeholder: '搜索歌曲、歌手、专辑...',
      searching: '搜索中...',
      search: '搜索',
      songs: '歌曲',
      artists: '歌手',
      albums: '专辑',
      noResults: '暂无搜索结果，请尝试其他关键词',
      song: '歌曲',
      artist: '歌手',
      album: '专辑',
      duration: '时长',
      songsCount: '首歌曲',
      searchFailed: '搜索失败，请稍后重试',
      cannotPlay: '该歌曲无法播放',
      playFailed: '播放歌曲失败'
    },
    // 歌曲详情页面
    songDetail: {
      loading: '正在加载歌曲详情...',
      artist: '歌手',
      album: '专辑',
      lyrics: '歌词',
      recommendations: '相关推荐',
      addToPlaylist: '添加到播放列表',
      download: '下载',
      downloadNotImplemented: '下载功能暂未实现',
      addedToPlaylist: '已添加到播放列表',
      alreadyInPlaylist: '该歌曲已在播放列表中',
      play: '播放',
      pause: '暂停',
      favorite: '收藏',
      fetchFailed: '获取歌曲详情失败，请稍后重试',
      noLyrics: '暂无歌词',
      addFailed: '添加失败，请稍后重试',
      cannotPlay: '无法播放该歌曲'
    },
    // 首页
    home: {
      popularPlaylists: '热门歌单',
      recentSongs: '最近播放',
      topCharts: '排行榜',
      recommendedForYou: '为你推荐',
      searchSongs: '搜索歌曲、歌手或专辑',
      createPlaylist: '创建播放列表'
    },
    // 播放器
    player: {
      playlists: '播放列表',
      repeat: '重复',
      shuffle: '随机',
      clearQueue: '清空队列',
      autoplay: '自动播放',
      loop: '循环'
    }
  }
};

// 获取本地存储的语言设置，默认为中文
const savedLocale = localStorage.getItem('language') || 'zh';

// 创建i18n实例
const i18n = new VueI18n({
  locale: savedLocale, // 设置默认语言
  messages, // 设置语言包
  fallbackLocale: 'zh', // 回退语言
  silentTranslationWarn: true, // 禁止翻译警告
  formatFallbackMessages: true // 启用格式回退消息
});

// vue-i18n插件已自动将i18n实例挂载到Vue原型上

// 导出i18n实例
export default i18n;

// 切换语言的方法
export function changeLanguage(lang) {
  if (messages[lang]) {
    i18n.locale = lang;
    localStorage.setItem('language', lang);
    return true;
  }
  return false;
}

// 获取当前语言
export function getCurrentLanguage() {
  return i18n.locale;
}

// 检查是否支持某语言
export function isLanguageSupported(lang) {
  return !!messages[lang];
}

// 获取所有支持的语言
export function getSupportedLanguages() {
  return Object.keys(messages);
}